﻿using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using FBEntities.FBObjects;
using XDAFacebook;
using XFControls;
using XFControls.Utils;
using XFControls.XFPanels;
using XFControls.XFPanels.XFPanelItems;

namespace XFItemControls.XFPanels.XFPanelItems
{
    public class XFItemPictureRow : XFItemBase
    {
        private readonly int _imageHeight;
        private readonly List<FBPhoto> _photos;
        private Rectangle _blackout;
        private int _mouseDown;
        private int _padding = 5;

        public XFItemPictureRow(List<FBPhoto> photos)
        {
            Style = new XFItemStyle
                        {
                            BackgroundColor = Color.White
                        };

            _photos = photos;
            _imageHeight = GetWidth()/Settings.PhotosPerLine;
            _imageHeight -= _padding*2;

            ItemType = XFItemType.ClickableNonChange;
        }

        public override int GetHeight()
        {
            return _imageHeight + (_padding*2);
        }

        public override void ItemPaint(Graphics g, int x, int y)
        {
            int currX = _padding;
            var rec = new Rectangle(currX, _padding, _imageHeight, _imageHeight);
            foreach (FBPhoto photo in _photos)
            {
                string url = "";
                foreach (FBImage img in photo.images)
                {
                    if (img.width < _imageHeight || img.height < _imageHeight)
                    {
                        url = img.source;
                        break;
                    }
                }

                if (string.IsNullOrEmpty(url))
                    url = photo.images.Last().source;

                rec.X = currX;
                DrawImage(url, g, rec);
                currX += _imageHeight + (_padding*2);
            }
        }

        public override void OnMouseDown(int x, int y)
        {
            base.OnMouseDown(x, y);
            _mouseDown = x/_imageHeight;
        }

        public override XFItemClickResult GetClickResult()
        {
            //Process.Start(_photos[_mouseDown].images[0].source, null);
            using (var viewer = new ImageViewer(_photos[_mouseDown]))
            {
                var ret = viewer.ShowDialog();
                if(ret == DialogResult.Yes)
                {
                    if (Parent == null)
                        return XFItemClickResult.Nothing;

                    if (Parent.Name == Settings.LocalizationStrings.postDetailsHeaderTitle)
                        return XFItemClickResult.Nothing;

                    XFPanelList rp = XFItemPost.GetCommentsPanel(_photos[_mouseDown].ToPost());

                    Parent.SetRightPanel(rp);

                    rp.ShowAlphaBar(false);
                    rp.ShowScrollbar(true);
                    rp.ScrollbarColor = Parent.ScrollbarColor;

                    return XFItemClickResult.SlideLeft;
                }
            }
            return base.GetClickResult();
        }
    }
}